<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login()
    {
        return view('admin.login');
    }
    public function dologin(Request $request)
    {
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password]))
        {
            return json_encode(['success'=>true,'msg'=>"成功登录"]);
        }
        else
        {
            return json_encode(['success'=>false,'msg'=>"用户名或密码错误，请重新登录"]);
        }
    }
    public function logout()
    {
        Auth::logout();
        return redirect('/login');
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->all();
        $rules=[
            'name'=>'required|min:3|alpha_dash|unique:users',
            //'password'=>'required|email',
            'password'=>'required|min:6|confirmed'
        ];
        $messages=[
            'name.required'=>'用户名不能为空',
            'name.min'=>'用户名不能少于:min个字符',
            'name.alpha_dash'=>'用户名只能由字母、数组、横线、下划线组成',
            'name.unique'=>'用户名已经存在，不能重复',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致'
        ];
        $validator=Validator::make($data,$rules,$messages);
        if($validator->fails()){
            $msg=$validator->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else{
            $data['password']=Hash::make($data['password']);
            User::create($data);
            return json_encode(['success'=>true,'msg'=>"success"]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user=User::find($id);
        return view('admin.user_edit')->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $messages=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致'
        ];
        $validator=Validator::make($data,$rules,$messages);
        if($validator->fails()){
            $msg=$validator->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else{
            $data['password']=Hash::make($data['password']);
            $user=User::find($id);
            $user->password=$data['password'];
            $user->save();
            return json_encode(['success'=>true,'msg'=>"success"]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user=User::find($id);
        $user->delete();
        return json_encode(['success'=>true,'msg'=>'success']);

    }
    public function list(){
        $users=User::paginate(5);
        return view('admin.user_list')->with('users',$users);

    }
}
